package client;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
/**
 * Formulaire pour creer un nouveau compte
 * @author CONSTANTIN Laurent
 * @version 1.0
 */
public class JPanelNewAccount extends JPanel{
	static final long serialVersionUID = -2704015582544368311L;
	// Application client
	private final ClientApplication clientApp;
	// Zone de saisies
    private JTextField inputUser = new JTextField("");
    private JTextField inputPassword = new JTextField("");
    private JTextField inputPasswordConfirm = new JTextField("");
    // Boutons
    private JButton btnOK = new JButton("Creer un compte");
    private JButton btnLoginForm = new JButton("Utiliser un compte existant");

    /**
     * Verifie le formulaire
     * et envoie la demande de creation de compte au serveur
     */
    public void createAccount(){
    	// Verifie que les champs ne sont pas vides
    	if(inputUser.getText().trim().isEmpty() || inputPassword.getText().trim().isEmpty() || inputPasswordConfirm.getText().trim().isEmpty())
    		JOptionPane.showMessageDialog(this, "Vous devez remplir le formulaire","Erreur de saisie",JOptionPane.ERROR_MESSAGE);
    	// verifie la confirmation de mot de passe
    	else if(! inputPassword.getText().equals(inputPasswordConfirm.getText()))
    		JOptionPane.showMessageDialog(this, "Les mots de passes ne correspondent pas","Erreur de saisie",JOptionPane.ERROR_MESSAGE);
    	else
    		// Creer un compte
    		clientApp.createAccount(inputUser.getText(), inputPassword.getText());
    }
    /**
     * Constructuer
     * @param clientApp L'application client
     */
    public JPanelNewAccount(ClientApplication clientApp){
    	
    	this.clientApp = clientApp;
        // Conteneurs pour le design
    	JPanel haut = new JPanel();
        haut.setLayout(new GridLayout(1,2));
        JPanel milieu = new JPanel();
        milieu.setLayout(new GridLayout(1,2));
        JPanel bas = new JPanel();
        bas.setLayout(new GridLayout(1,2));
        setLayout(new BoxLayout(this,2));
               
        // Tailles
        setPreferredSize(new Dimension(250,180));
        inputUser.setPreferredSize(new Dimension(100,20));
        inputPassword.setPreferredSize(new Dimension(100,20));
        inputPasswordConfirm.setPreferredSize(inputPassword.getPreferredSize());

        // Action sur les boutons
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	JPanelNewAccount.this.createAccount();
            }
        });
        btnLoginForm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	JPanelNewAccount.this.clientApp.getClientView().showLoginDialog();
            }
        });
        
        
        setLayout(new FlowLayout());

        // Design
        add(new JLabel("Creation de compte :"));
        haut.add(new JLabel("Pseudo : "),LEFT_ALIGNMENT);
        haut.add(inputUser,RIGHT_ALIGNMENT);
        
        milieu.add(new JLabel("Mot de passe : "),LEFT_ALIGNMENT);
        milieu.add(inputPassword,RIGHT_ALIGNMENT);
        
        bas.add(new JLabel("Confirmation : "),LEFT_ALIGNMENT);
        bas.add(inputPasswordConfirm,RIGHT_ALIGNMENT);
        
        add(haut);
        add(milieu);
        add(bas);

        add(btnOK,RIGHT_ALIGNMENT);
        add(btnLoginForm,LEFT_ALIGNMENT);

    }
}
